// CopyManager.cpp : implementation file
//

#include <stdafx.h>
#include "SwordBible.h"
#include "SwordBibleDoc.h"
#include "CopyManager.h"
#include "BiblePage.h"
#include "NotesPage.h"
#include "MainFrm.h"

// CCopyManager dialog

IMPLEMENT_DYNAMIC(CCopyManager, CDialog)

CCopyManager::CCopyManager(CWnd* pParent /*=NULL*/)
	: CDialog(CCopyManager::IDD, pParent)
{
	first = 0; // the first format (radio button) will be selected by defualt

}

CCopyManager::~CCopyManager()
{
}

void CCopyManager::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Radio(pDX,IDC_F1,first);
	DDX_Control(pDX, IDC_COMBO_BOOKS, m_comboBook);
	DDX_Control(pDX, IDC_COMBO_CHAPTERS, m_comboChapter);
	DDX_Control(pDX, IDC_COMBO_STARTVERSE, m_comboStart);
	DDX_Control(pDX, IDC_COMBO_ENDVERSE, m_comboEnd);
	DDX_Control(pDX, IDC_RICHEDIT_CM, m_rich);
	DDX_Control(pDX, IDC_CHECK_REFERENCE, m_refCtrl);
}


BEGIN_MESSAGE_MAP(CCopyManager, CDialog)
	ON_COMMAND_RANGE(IDC_F1, IDC_F6, OnFormatChange)
	ON_CBN_SELCHANGE(IDC_COMBO_BOOKS, &CCopyManager::OnSelchangeBooks)
	ON_CBN_SELCHANGE(IDC_COMBO_CHAPTERS, &CCopyManager::OnSelchangeChapter)
	ON_CBN_SELCHANGE(IDC_COMBO_STARTVERSE, &CCopyManager::OnSelChangeStart)
	ON_CBN_SELCHANGE(IDC_COMBO_ENDVERSE, &CCopyManager::OnSelChangeEnd)
	ON_BN_CLICKED(IDC_CHECK_REFERENCE, &CCopyManager::OnClickAbbreviate)
	ON_BN_CLICKED(IDC_BUTTON_STUDYNOTES, &CCopyManager::OnStudyNotes)
	ON_BN_CLICKED(IDC_BUTTON_CLIPBOARD, &CCopyManager::OnClipboard)
	ON_BN_CLICKED(IDC_BUTTON_VERSELIST, &CCopyManager::OnVerseList)
END_MESSAGE_MAP()


// CCopyManager message handlers

BOOL CCopyManager::OnInitDialog()
{
	CDialog::OnInitDialog();


	/* Fill in the names of the Bible book in the Bible book combo */
	for( int i = 0; i < 66; i++)
	m_comboBook.AddString(CSwordBibleDoc::bookCode[i][0]);

	// set the selection to the current book
	book = CSwordBibleDoc::Book() - 1;
	m_comboBook.SetCurSel(book);


	//find the number of chapters in the currently selected book and fill the combo box
	int totalchap =  _tstoi( CSwordBibleDoc::bookCode[book][2] );
	 CString number;
	 for(int i = 0; i < totalchap; i++) {
		 number.Format(_T("%d"),i+1);			 
		 m_comboChapter.AddString(number);
	 }

	// set the chapter in the combo box
	 chap = CSwordBibleDoc::Chapter() - 1;
	 m_comboChapter.SetCurSel(chap);	
	 m_comboChapter.LimitText(3);


 // get the total number of verses in the newly selected chapter
	int totalverses = CSwordBibleDoc::VerseCount[book][chap];
	 
	
	 for(int i = 0; i < totalverses; i++) {
		 number.Format(_T("%d"),i+1);			 
		 m_comboStart.AddString(number);
		 m_comboEnd.AddString(number);
	 }

	// set the verse in the combo boxes
	 start = CSwordBibleDoc::Verse() - 1;
	 m_comboStart.SetCurSel(start);	
	 m_comboStart.LimitText(3);

	 end = start;
	 m_comboEnd.SetCurSel(end);	
	 m_comboEnd.LimitText(3);

	 m_refCtrl.SetCheck(1);

	 Preview();


	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CCopyManager::OnFormatChange(UINT nID)
{
	UpdateData(TRUE);  //<-- changes the value of variable first
	Preview(); 

	

}

void CCopyManager::Preview()
{

	preview = CBiblePage::font.GetHeaderRTF();

	int s = start;
	if(end < start) s = end;

	CString temp;
	temp.Format(_T("%s %d:%d"), CSwordBibleDoc::bookCode[book][0],chap + 1, s + 1);
	sword::VerseKey key = (CStringA) temp;

	int count = end - start;
	if(count < 0) count = -count;

	if(first == 3 || first == 4)
		preview = preview + _T("\\cf1 ") +  CSwordBibleDoc::bookCode[book][0] + _T(" \\par\\par ");

	if(first == 5)
		preview = preview + _T("\\cf2 ") +  CSwordBibleDoc::bookCode[book][0] + _T(" \\par\\par ");

	// abbreviate reference?
	CString ref = _T("");
	


	for(int i = 0; i <= count ; i++)
	{
		pModule->SetKey(key);  
		CString renderText = (CString) pModule->RenderText(); 

		CString verse;
		verse.Format(_T("%d"), key.Verse());

		if(m_refCtrl.GetCheck()) ref = key.getShortText();
		else ref = key.getText();


		// the verse use the color cf1 in the color table
			if(first == 0) preview = preview +  _T("\\cf1\\b ") + ref + _T(" \\b0 ");
			else if(first == 1) preview = preview + _T("\\cf1\\b ") + ref + _T(" \\b0\\par\\par "); 
			else if(first == 2) preview = preview + _T("\\cf1\\b [") + ref + _T("] \\b0 ");
			else if(first == 3) preview = preview + _T("\\cf1\\super\\b ") + verse + _T(" \\b0\\nosupersub "); 
			else if(first == 4) preview = preview + _T("\\cf2 ") + verse + _T(".");
			

			// the text uses the color cf2 in the color table
			preview += _T("\\cf2 ");

			renderText.Replace(_T("\\par"), _T(" "));
			renderText.Replace(_T("\\cf6"),_T("\\cf9"));

			preview += renderText;

			if(first == 0 || first == 1 || first == 2) preview += _T("\\par\\par ");	// each verse on a separate line

			key++;

	}

	preview += _T("}");

	preview.Replace(_T("<a href=\"\">*n"),  _T("\\cf5\\b *n"));
	preview.Replace(_T("<a href=\"\">*x"),  _T("\\cf6\\b *x"));
	preview.Replace(_T("</a>"), _T("\\cf2\\b0 "));
	preview.Replace(_T("<a href=\"\">"),  _T(" "));

	m_rich.SetRTF(preview);


	
}

void CCopyManager::OnSelchangeBooks()
{
	book = m_comboBook.GetCurSel();

	//find the number of chapters in the currently selected book and fill the combo box
	while(m_comboChapter.GetCount() > 0) m_comboChapter.DeleteString(0); // empty

	 CString number;
	 int totalchap =  _tstoi( CSwordBibleDoc::bookCode[book][2] );
	 for(int i = 0; i < totalchap; i++) {
		 number.Format(_T("%d"),i+1);			 
		 m_comboChapter.AddString(number);
	 }

	 // set the chapter to 1
	 m_comboChapter.SetCurSel(0);
	 OnSelchangeChapter();

}

void CCopyManager::OnSelchangeChapter()
{
	chap = m_comboChapter.GetCurSel();

	// fill in the start and end combo boxes

	 // get the total number of verses in the newly selected chapter
	int totalverses = CSwordBibleDoc::VerseCount[book][chap];
	 
	while(m_comboStart.GetCount() > 0) m_comboStart.DeleteString(0);
	while(m_comboEnd.GetCount() > 0) m_comboEnd.DeleteString(0);

	 
	CString number;
	 for(int i = 0; i < totalverses; i++) {
		 number.Format(_T("%d"),i+1);			 
		 m_comboStart.AddString(number);
		 m_comboEnd.AddString(number);
	 }

	 // set the start and end verses to 1
	 m_comboStart.SetCurSel(0);
	 OnSelChangeStart();


	 m_comboEnd.SetCurSel(0);
	 OnSelChangeEnd();

	
}

void CCopyManager::OnSelChangeStart()
{
	start = m_comboStart.GetCurSel();

	Preview();
	
}

void CCopyManager::OnSelChangeEnd()
{
	end = m_comboEnd.GetCurSel();

	Preview();
	
}

void CCopyManager::OnClickAbbreviate()
{
	UpdateData(TRUE);
	Preview();
}

void CCopyManager::OnStudyNotes()
{
	m_rich.SetSel(0,-1);
	m_rich.Copy();
	m_rich.SetSel(0,0);

	CNotesPage::ActiveMainFrameCommand = _T("paste");
	AfxGetMainWnd()->SendMessageToDescendants(ID_NOTES_COMMAND,0,0);

	//AfxMessageBox("Verse range sent to study notes"); <-- no need, since the changes are clearly visible
	// in the notes editor

	OnCancel();
	
}

void CCopyManager::OnClipboard()
{
	m_rich.SetSel(0,-1);
	m_rich.Copy();
	m_rich.SetSel(0,0);

	AfxMessageBox(_T("Verse range sent to clipboard."));

	OnCancel();

	
}

void CCopyManager::OnVerseList()
{
		// create the verse list window if it does not exist
	if (!CMainFrame::m_pModelessVL)
       CMainFrame::m_pModelessVL= new CVerseLists;

   if (!::IsWindow(CMainFrame::m_pModelessVL->GetSafeHwnd()))
	   CMainFrame::m_pModelessVL->Create(IDD_VERSELISTS, (CMainFrame*) AfxGetApp()->GetMainWnd() );

   
   int s = start;
   int e = end;
   if(end < start) {s = end; e = start;}

	CString temp;
	temp.Format(_T("%s %d:%d - %s %d:%d"), CSwordBibleDoc::bookCode[book][0],chap + 1, s + 1,
		CSwordBibleDoc::bookCode[book][0],chap + 1, e+1);

	BOOL res = FALSE;
	res = CMainFrame::m_pModelessVL->SaveVerseRange(temp);


	if(res == TRUE) AfxMessageBox(_T("Verse range sent to verse lists."));

	OnCancel();	
}
